<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Controller;
use App\Models\UserExtract;
use App\Traits\Controllers\IndexTraits;
use App\Traits\Controllers\UpdateTraits;
use App\Transformers\User\UserExtractsTransformer;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserExtractController extends Controller
{
    use IndexTraits;
    use UpdateTraits;

    public $time;
    public $start;
    public $stop;

    public $model = UserExtract::class;

    public $transformer = UserExtractsTransformer::class;

    /**
     * 参数验证规则
     *
     * @param Request $request
     * @return array
     */
    public function rules(Request $request): array
    {
        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'POST':
                return [];
                break;
            case 'PUT':
                return [
                    'real_name' => 'required',
                    'extract_price' => 'required',
                    'wechat_code' => 'required',
                ];
                break;
            case 'PATCH':
                return [];
                break;
        }
        return [];
    }

    /**
     * 验证信息
     *
     * @return array
     */
    public function ruleMessages(): array
    {
        return [
            'real_name.required' => '姓名不能为空',
            'extract_price.required' => '价格不能为空',
            'wechat_code.required' => '微信号不能为空',
        ];
    }



    public function index(Request $request)
    {
        //先处理传过来的搜索数据
        $this->formatIndexQuery($request);
        //获取传过来的时间类型
        $this->time = $request->input('time');
        //获取开始时间和结束时间
        $this->start = $request->input('start');
        $this->stop = $request->input('stop');
        $data = $this->aa($this->time,$this->start,$this->stop)->paginate($this->getPageLimit());

        return $this->response->paginator($data, resolve($this->transformer));
    }


    /**
     * 提现申请列表总金额
     */
    public function totalPrice(Request $request)
    {
        //获取传过来的时间类型
        $this->time = $request->input('time');
        //获取开始时间和结束时间
        $this->start = $request->input('start');
        $this->stop = $request->input('stop');
        $search = $this->aa($this->time,$this->start,$this->stop)->get();
        $balance = $underreview_price = $withdrawncash_price = $fail_price = $aa = 0;
        foreach ($search as $k=>$v){
            $balance += $v->balance;//佣金总金额

            $aa += $v->extract_price;
            //待提现金额
            if($v->status =='underreview'){
                $underreview_price += $v->extract_price;
            }

            //已提现金额
            if($v->status == 'withdrawncash'){
                $withdrawncash_price += $v->extract_price;
            }

            //未提现金额
            if($v->status == 'fail'){
                $fail_price += $v->extract_price;
            }
        }
        $arr =[];
        $arr['underreview_price'] = $underreview_price;
        $arr['balance'] = $balance;
        $arr['withdrawncash_price'] = $withdrawncash_price;
        $arr['fail_price'] = $fail_price;



        return $this->response->array($arr);
    }

    /**
     * 审核通过
     */
    public function adopt(UserExtract $userExtract)
    {
        $userExtract->status = 'withdrawncash';
        $userExtract->save();
        return $this->response->noContent();
    }

    /**
     * 审核无效
     */
    public function invalid(UserExtract $userExtract)
    {
        $userExtract->status = 'fail';
        $userExtract->save();
        return $this->response->noContent();
    }

    public function aa($time,$start,$stop){
        return $this
            ->getQuery()
            ->when($time || $start || $stop, function ($query) use ($time,$start,$stop) {
                //自定义时间
                if(!empty($start) && !empty($stop)){
                    $query->whereBetween('created_at',[$start,$stop]);
                }

                //搜索今天数据
                if ($time == "today") {
                    $query->whereBetween('created_at', [Carbon::now()->startOfDay(), Carbon::now()->endOfDay()]);
                }

                //搜索昨天数据
                if ($time == "yesterday") {
                    $query->whereDay('created_at', [Carbon::now()->yesterday(), Carbon::now()->startOfDay()]);
                }

                //搜索本周数据
                if ($time == "week") {
                    $query->whereBetween('created_at', [Carbon::now()->startOfweek(),Carbon::now()->endOfweek()]);
                }

                //搜索本季度数据
                if ($time == "quarter") {
                    $query->whereBetween('created_at', [Carbon::now()->startOfQuarter(),Carbon::now()->endOfQuarter()]);
                }

                //搜索本月数据
                if ($time == "month") {
                    $query->whereBetween('created_at', [Carbon::now()->startOfMonth(),Carbon::now()->endOfMonth()]);
                }

                //搜索本年数据
                if ($time == "year") {
                    $query->whereBetween('created_at', [Carbon::now()->startOfYear(), Carbon::now()->endOfYear()]);
                }
            })
            ->pimp();
    }
}
